# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html

# Sets the minimum version of CMake required to build the native library.

cmake_minimum_required(VERSION 3.4.1)

# Declares and names the project.

project("jrtplib")


include_directories(.)
include_directories(JRTPLIB/src)
include_directories(jthread)

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "JRTPLIB/cmake")

include(CheckCXXCompilerFlag)
include(CheckIncludeFile)
include(CheckIncludeFileCXX)
include(CheckCXXSourceCompiles)
include(TestBigEndian)
include(JRTPLIB/cmake/Macros.cmake)

set(VERSION_MAJOR 3)
set(VERSION_MINOR 11)
set(VERSION_DEBUG 2)
set(VERSION "${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_DEBUG}")

set(JRTPLIB_LINK_LIBS "")
set(JRTPLIB_INTERNAL_INCLUDES "")
set(JRTPLIB_EXTERNAL_INCLUDES "")
set(JRTPLIB_WINSOCK FALSE)
set (RTP_SUPPORT_THREAD "#define RTP_SUPPORT_THREAD")

jrtplib_support_option("Support SDES PRIV items" JRTPLIB_SUPPORT_SDESPRIV RTP_SUPPORT_SDESPRIV ON "// No support for SDES PRIV items")
jrtplib_support_option("Support the probation mechanism for a new source" JRTPLIB_SUPPORT_PROBATION RTP_SUPPORT_PROBATION ON "// Do not wait for a number of consecutive packets to validate source")
jrtplib_support_option("Support sending RTCP APP packets" JRTPLIB_SUPPORT_SENDAPP RTP_SUPPORT_SENDAPP ON "// No direct support for sending RTCP APP packets")
jrtplib_support_option("Support sending unknown RTCP packets" JRTPLIB_SUPPORT_RTCPUNKNOWN RTP_SUPPORT_RTCPUNKNOWN OFF "// No support for sending unknown RTCP packets")
jrtplib_support_option("Support memory management mechanism" JRTPLIB_SUPPORT_MEMORYMGMT RTP_SUPPORT_MEMORYMANAGEMENT ON "// No memory management support")

jrtplib_include_test(sys/filio.h RTP_HAVE_SYS_FILIO "// Don't have <sys/filio.h>")
jrtplib_include_test(sys/sockio.h RTP_HAVE_SYS_SOCKIO "// Don't have <sys/sockio.h>")
jrtplib_include_test(netinet/in.h RTP_SUPPORT_NETINET_IN "// Don't have <netinet/in.h>")

jrtplib_test_feature(socklentest RTP_SOCKLENTYPE_UINT TRUE "// socklen_t is 'int'" "${TESTDEFS}")
jrtplib_test_feature(ipv4mcasttest RTP_SUPPORT_IPV4MULTICAST FALSE "// No IPv4 multicasting support" "${TESTDEFS}")
jrtplib_test_feature(salentest RTP_HAVE_SOCKADDR_LEN FALSE "// No sa_len member in struct sockaddr" "${TESTDEFS}")
jrtplib_test_feature(getloginrtest RTP_SUPPORT_GETLOGINR FALSE "// Not using getlogin_r" "${TESTDEFS}")
jrtplib_test_feature(ipv6test RTP_SUPPORT_IPV6 FALSE "// No IPv6 support" "${TESTDEFS}")
jrtplib_test_feature(ipv6mcasttest RTP_SUPPORT_IPV6MULTICAST FALSE "// No IPv6 multicasting support" "${TESTDEFS}")
jrtplib_test_feature(performancecounter RTP_HAVE_QUERYPERFORMANCECOUNTER FALSE "// No QueryPerformanceCounter support" "${TESTDEFS}")
jrtplib_test_feature(suffix RTP_HAVE_VSUINT64SUFFIX FALSE "// No ui64 suffix" "${TESTDEFS}")
jrtplib_test_feature(arrayalloc RTP_HAVE_ARRAYALLOC FALSE "// No custom new[]" "${TESTDEFS}")
jrtplib_test_feature(randstest RTP_HAVE_RAND_S FALSE "// No rand_s support" "${TESTDEFS}")
jrtplib_test_feature(strncpystest RTP_HAVE_STRNCPY_S FALSE "// No strncpy_s support" "${TESTDEFS}")
jrtplib_test_feature(clockgettimetest RTP_HAVE_CLOCK_GETTIME FALSE "// No clock_gettime support" "${TESTDEFS}")
jrtplib_test_feature(polltest RTP_HAVE_POLL FALSE "// No 'poll' support" "${TESTDEFS}")
jrtplib_test_feature(wsapolltest RTP_HAVE_WSAPOLL FALSE "// No 'WSAPoll' support" "${TESTDEFS}")
jrtplib_test_feature(msgnosignaltest RTP_HAVE_MSG_NOSIGNAL FALSE "// No MSG_NOSIGNAL option" "${TESTDEFS}")
jrtplib_test_feature(ifaddrstest RTP_SUPPORT_IFADDRS FALSE "// No ifaddrs support" "${TESTDEFS}")

check_cxx_source_compiles("#include <stdint.h>\n#include <sys/types.h>\nint main(void) { size_t a = 0 ; size_t b = a; uint32_t x = 0; uint32_t y = x; return 0; }" JRTPLIB_STDINT)
if (JRTPLIB_STDINT)
    set(RTP_INTTYPE_HEADERS "#include <stdint.h>\n#include <sys/types.h>")
else (JRTPLIB_STDINT)
    check_cxx_source_compiles("#include <inttypes.h>\n#include <sys/types.h>\nint main(void) { uint32_t x = 0; uint32_t y = x; return 0; }" JRTPLIB_INTTYPES)
    if (JRTPLIB_INTTYPES)
        set(RTP_INTTYPE_HEADERS "#include <inttypes.h>\n#include <sys/types.h>\n")
    else (JRTPLIB_INTTYPES)
        if (MSVC)
            set(RTP_INTTYPE_HEADERS "#include \"rtptypes_win.h\"")
        else (MSVC)
            set(RTP_INTTYPE_HEADERS "#error Could not find header files that define types like 'uint32_t'. Please edit the file ${PROJECT_BINARY_DIR}/src/rtptypes_unix.h and make sure that the following types are defined: int8_t, uint8_t, int16_t, uint16_t, int32_t, uint32_t, int64_t, uint64_t")
            message("\n\nError: Could not find header files that define types like 'uint32_t'.\nPlease edit the file ${PROJECT_BINARY_DIR}/src/rtptypes_unix.h\nand make sure that the following types are defined: \nint8_t, uint8_t, int16_t, uint16_t, int32_t, uint32_t, int64_t, uint64_t\n\n")
        endif (MSVC)
    endif (JRTPLIB_INTTYPES)
endif (JRTPLIB_STDINT)


configure_file("JRTPLIB/src/rtptypes.h.in" "${PROJECT_BINARY_DIR}/config/rtptypes.h")
configure_file("JRTPLIB/src/rtpconfig.h.in" "${PROJECT_BINARY_DIR}/config/rtpconfig.h")
configure_file("JRTPLIB/src/rtplibraryversioninternal.h.in" "${PROJECT_BINARY_DIR}/config/rtplibraryversioninternal.h")

save_paths(JRTPLIB_INTERNAL_INCLUDES "JRTPLIB/src" "JRTPLIB/src")
include_directories(${PROJECT_BINARY_DIR}/config)

# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.

add_library( # Sets the name of the library.
             jrtplib_jni

             # Sets the library as a shared library.
             SHARED

             # Provides a relative path to your source file(s).
             jthread/jmutex.cpp
             jthread/jthread.cpp

             JRTPLIB/src/rtpudpv6transmitter.cpp
             JRTPLIB/src/rtprandomrand48.cpp
             JRTPLIB/src/rtcppacket.cpp
             JRTPLIB/src/rtptcpaddress.cpp
             JRTPLIB/src/rtprandom.cpp
             JRTPLIB/src/rtpcollisionlist.cpp
             JRTPLIB/src/rtprandomrands.cpp
             JRTPLIB/src/rtpexternaltransmitter.cpp
             JRTPLIB/src/rtpudpv4transmitter.cpp
             JRTPLIB/src/rtpipv6address.cpp
             JRTPLIB/src/rtpsourcedata.cpp
             JRTPLIB/src/rtppacket.cpp
             JRTPLIB/src/rtpbyteaddress.cpp
             JRTPLIB/src/rtcpscheduler.cpp
             JRTPLIB/src/extratransmitters/rtpfaketransmitter.cpp
             JRTPLIB/src/rtpsession.cpp
             JRTPLIB/src/rtpdebug.cpp
             JRTPLIB/src/rtcpapppacket.cpp
             JRTPLIB/src/rtptcptransmitter.cpp
             JRTPLIB/src/rtplibraryversion.cpp
             JRTPLIB/src/rtcpcompoundpacketbuilder.cpp
             JRTPLIB/src/rtcpsrpacket.cpp
             JRTPLIB/src/rtcpbyepacket.cpp
             JRTPLIB/src/rtppollthread.cpp
             JRTPLIB/src/rtcppacketbuilder.cpp
             JRTPLIB/src/rtpsessionparams.cpp
             JRTPLIB/src/rtpipv4address.cpp
             JRTPLIB/src/rtpsessionsources.cpp
             JRTPLIB/src/rtperrors.cpp
             JRTPLIB/src/rtprandomurandom.cpp
             JRTPLIB/src/rtcprrpacket.cpp
             JRTPLIB/src/rtcpsdespacket.cpp
             JRTPLIB/src/rtpsources.cpp
             JRTPLIB/src/rtptimeutilities.cpp
             JRTPLIB/src/rtpinternalsourcedata.cpp
             JRTPLIB/src/rtppacketbuilder.cpp
             JRTPLIB/src/rtpsecuresession.cpp
             JRTPLIB/src/rtpabortdescriptors.cpp
             JRTPLIB/src/rtcpcompoundpacket.cpp
             JRTPLIB/src/rtpipv4destination.cpp
             JRTPLIB/src/rtcpsdesinfo.cpp
             JRTPLIB/src/rtpipv6destination.cpp

             RtpCommon.cpp
             RtpReceiver.cpp
             RtpSender.cpp

             jrtplib_jni.cpp )

# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.

target_link_libraries( # Specifies the target library.
                       jrtplib_jni

                       # Links the target library to the log library
                       # included in the NDK.
                       log z android m )
